package com.xinsoft.controller.mes;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesStationPageParam;
import com.xinsoft.entity.po.MesStation;
import com.xinsoft.service.MesStationService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/mes/station")
public class MesStationController {

    @Autowired
    private MesStationService mesStationService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody MesStation mesStation) {
        // 参数校验
        ValidatorUtils.validateEntity(mesStation);
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MES_STATION_DOCUMENT);
        return Response.succeed(mesStationService.addMesStation(mesStation));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  MesStation mesStation) {
        // 参数校验
        ValidatorUtils.validateEntity(mesStation);
        return Response.succeed(mesStationService.editMesStation(mesStation));
    }

    /**
     * 获取工位
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(mesStationService.getMesStation(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        mesStationService.del(ids);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        mesStationService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        mesStationService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 导入
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) throws IOException {
        return Response.succeed(mesStationService.importExcel(multipartFile,response));
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody MesStationPageParam mesStationPageParam) {
        return Response.succeed(mesStationService.findPageListByParam(mesStationPageParam));
    }
}
